package com.nolon.flange.extensions

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.AutoCompleteTextView
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.google.android.material.textfield.TextInputEditText

val TextInputEditText.numberF: Float
    get() = if (text.isNullOrEmpty()) 0f else text.toString().toFloat()

fun TextInputEditText.numberF(default: Float = 0f) =
    if (text.isNullOrEmpty()) default else text.toString().toFloat()

fun AutoCompleteTextView.numberI(default: Int) =
    if (text.isNullOrEmpty()) default else text.toString().toInt()


fun AutoCompleteTextView.numberF(default: Float) =
    if (text.isNullOrEmpty()) default else text.toString().toInt()

val TextInputEditText.numberI: Int
    get() {
        val str = this.text.toString()
        return if (str.isEmpty()) 0
        else str.toInt()
    }

fun toast(context: Context?, message: String, duration: Int = Toast.LENGTH_SHORT) {
    context?.let {
        Toast.makeText(it, message, duration).apply {
            setGravity(Gravity.CENTER, 0, 0)
            show()
        }
    }
}

fun View.toast(strId: Int) {
    context?.let {
        Toast.makeText(it, it.getString(strId), Toast.LENGTH_SHORT).apply {
            setGravity(Gravity.CENTER, 0, 0)
            show()
        }
    }
}

fun View.shortToast(message: Int) {
    context?.let {
        Toast.makeText(it, it.getString(message), Toast.LENGTH_SHORT).apply {
            setGravity(Gravity.CENTER, 0, 0)
            show()
        }
    }
}


val TextView.numberI: Int
    get() = if (text.isNullOrEmpty()) 0 else text.toString().toInt()


val TextView.numberF: Float
    get() = if (text.isNullOrEmpty()) 0f else text.toString().toFloat()


fun EditText.numberF(default: Float = 0f) =
    if (text.isNullOrEmpty()) default else text.toString().toFloat()

fun EditText.numberI(default: Int = 5) =
    if (text.isNullOrEmpty()) default else text.toString().toInt()
